<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;
use app\common\model\Type as TypeModel;

class Type extends Admin {

    public $typeModel;

    public function __construct(Request $request = null) {
        parent::__construct($request);
        $this->typeModel = new TypeModel();
    }

    /**
     * 分类管理页
     * @return type
     */
    public function index() {
        $list = $this->typeModel->getList();
        //var_dump(json_encode($list));
        $this->assign('type', json_encode($list));
        return $this->fetch();
    }

    /**
     * 添加或编辑分类
     * @param Request $request
     * @return type
     */
    public function editType(Request $request) {
        $data = $request->param();

        if ($data['id']) {
            TypeModel::where('id', $data['id'])->update($data);
        } else {
            TypeModel::insert($data);
        }

        $list = $this->typeModel->getList();
        $this->success('sss', '', $list);
    }

    /**
     * 删除菜单
     * @return type
     */
    public function delType() {
        $data = $this->request->param();
        TypeModel::where('id', $data['id'])->delete();
        $list = $this->typeModel->getList();
        return $this->success('success', '', $list);
    }

    /**
     * 更新排序
     * @param Request $request
     * @return type
     */
    public function setSort(Request $request) {
        $data = $request->param();

        foreach ($data as $key => $typeid) {
            TypeModel::where('id', $typeid)->update(['list_order' => $key]);
        }

        return $this->success('success', '', $data);
    }

}
